package com.qiusuo0226.self.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiusuo0226.common.constant.NumConstant;
import com.qiusuo0226.common.util.AlgorithmTool;
import com.qiusuo0226.mvc.util.JsonResultModel;
import com.qiusuo0226.mysql.property.MybatisPlusProperty;
import com.qiusuo0226.self.mapper.BillCategoryMapper;
import com.qiusuo0226.self.service.BillCategoryService;
import com.qiusuo0226.self.service.BillRecordService;
import com.qiusuo0226.self.pojo.entity.BillCategoryDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 账单类型表 逻辑实现层
 * @author qiusuo
 * @date 2023/8/5 23:54
 */
@Service
public class BillCategoryServiceImpl extends ServiceImpl<BillCategoryMapper, BillCategoryDO> implements BillCategoryService {

    @Autowired
    private MybatisPlusProperty mybatisPlusProperty;

    @Autowired
    private BillRecordService billRecordService;

    @Override
    public JsonResultModel add(String creater, String billType, String billCategory) {
        int effectRows = baseMapper.add(AlgorithmTool.snowflake(mybatisPlusProperty.getGlobalConfig().datacenterId, mybatisPlusProperty.getGlobalConfig().workerId),
                                        creater,
                                        billType,
                                        billCategory,
                                        new Date());

        return effectRows > NumConstant.N0 ? JsonResultModel.success("新增成功") : JsonResultModel.failure("新增失败");
    }

    @Override
    public JsonResultModel list(String creater, String billType) {
        QueryWrapper<BillCategoryDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("iid, bill_type billType, bill_category billCategory");
        queryWrapper.eq("creater", creater);
        queryWrapper.eq("bill_type", billType);
        List<Map<String, Object>> list = baseMapper.selectMaps(queryWrapper);
        return JsonResultModel.success("获取账单类型列表成功", list);
    }

    @Override
    public JsonResultModel remove(String creater, String categoryIid) {
        List<String> iids = Arrays.asList(categoryIid.split(","));
        int effectRows = baseMapper.remove(creater, iids);
        return effectRows > NumConstant.N0 ? JsonResultModel.success("删除成功") : JsonResultModel.failure("删除失败");
    }

    @Override
    public JsonResultModel edit(String creater, String categoryIid, String billCategory) {
        // 搜索原值
        BillCategoryDO entity = baseMapper.selectById(categoryIid);
        // 当老账单类别等于新账单类别，则不需要修改修改
        if (StringUtils.equals(entity.getBillCategory(), billCategory)) {
            return JsonResultModel.success("编辑成功");
        }
        // 1、修改账单类别名称
        int effectRows = baseMapper.edit(creater, entity.getBillType(), categoryIid, billCategory);
        // 影响行数 大于0 则代表修改账单类型成功
        if (effectRows > NumConstant.N0) {
            // 2、修改账单记录中类别名称
            billRecordService.editRecordCategoryAll(creater, entity.getBillType(), entity.getBillCategory(), billCategory);
            return JsonResultModel.success("编辑成功");
        } else {
            return JsonResultModel.failure("编辑失败");
        }
    }
}
